package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService iYddAdvimgService;
    @Autowired
    private IYddAdvposService iYddAdvposService;

    // 广告图详情
    @GetMapping("/rowInfo")
    public R rowInfo(Long id) {
        log.info("查看{}的详情", id);
        YddAdvimg advimg = iYddAdvimgService.getById(id);
        R r = R.success("id", advimg.getId());
        r.getData().put("advimgpos", advimg.getAdvimgpos());
        r.getData().put("advimgsrc", advimg.getAdvimgsrc());
        r.getData().put("advimgdesc", advimg.getAdvimgdesc());
        r.getData().put("advimglink", advimg.getAdvimglink());
        r.getData().put("defaultData", advimg.getDefaultData());
        return r;
    }

    //分页查询
    @GetMapping("/list")
    public R list(String advimgdesc, String advimgpos, int page, int limit) {
        log.info("查找的广告位描述有{},名称含{},查询第{}页,每页数量为{}", advimgdesc, advimgpos, page, limit);
        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);
        Page<YddAdvimgDto> yddAdvimgDtoPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.like(advimgdesc != null, YddAdvimg::getAdvimgdesc, advimgdesc);
        lqw.like(advimgpos != null, YddAdvimg::getAdvimgpos, advimgpos);
        iYddAdvimgService.page(yddAdvimgPage, lqw);
        //对象拷贝,除了records里的数据,records数据还需要加广告图的数量,需要查询后封装
        BeanUtils.copyProperties(yddAdvimgPage, yddAdvimgDtoPage, "records");
        List<YddAdvimg> records = yddAdvimgPage.getRecords();
        List<YddAdvimgDto> list = new ArrayList<>();
        for (YddAdvimg record : records) {
            YddAdvimgDto dto = new YddAdvimgDto();
            BeanUtils.copyProperties(record, dto);
            YddAdvpos yddAdvpos = iYddAdvposService.getById(record.getAdvimgpos());
            dto.setAdvPosName(yddAdvpos.getAdvposname());
            list.add(dto);
        }
        yddAdvimgDtoPage.setRecords(list);
        R r = R.success("data", yddAdvimgDtoPage.getRecords());

        r.getData().put("totalPages", (int) yddAdvimgDtoPage.getPages());//总页数
        r.getData().put("pageSize", yddAdvimgDtoPage.getSize());//每页显示数
        r.getData().put("currentPage", yddAdvimgDtoPage.getCurrent());//当前页码值
        r.getData().put("count", (int) yddAdvimgDtoPage.getTotal());//总条数

        return r;
    }

    //修改信息
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg) {
        iYddAdvimgService.updateById(yddAdvimg);
        return R.success("", "");
    }

    //删除信息
    @GetMapping("/del")
    public R del(long id) {

        iYddAdvimgService.removeById(id);

        return R.success("", "");
    }

    //添加信息
    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg) {
        iYddAdvimgService.save(yddAdvimg);
        return R.success("", "");
    }
}
